/**
 * @file tiny_queue_plus.c
 * @brief 队列plus实现
 * @author 张恒宣
 * @LastEditTime 2024/9/21
 * @note 相较于邮箱和队列, 这个可以指定拷贝策略
 * 
 * MIT License
 *
 * Copyright (c) 2025 张恒宣 luckkeymail@163.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*********************
 *       头文件
 *********************/

#include "tiny_queue_plus.h"
#include "tiny_rtos_context.h"
#include "tiny_malloc.h"
#include "tiny_thread.h"

/*********************
 *      公共函数
 *********************/

/// @brief 创建队列
/// @param block_count 块数量
/// @param block_size 块大小
/// @param queue_data_copy 拷贝策略
/// @return 队列实例, 如果失败返回tiny_null
tiny_queue_plus_t *tiny_queue_plus_create(tiny_uint32_t block_count, tiny_uint32_t block_size, tiny_rq_copy_callback queue_data_copy)
{
	tiny_ring_queue_t *ring_queue = tiny_ring_queue_create(block_count, block_size, queue_data_copy);
	if (!ring_queue)
	{
		return tiny_null;
	}
	tiny_queue_plus_t *queue_obj = tiny_malloc(sizeof(tiny_queue_plus_t));
	if (!queue_obj)
	{
		tiny_ring_queue_free(ring_queue);
		return tiny_null;
	}

	tiny_init_list(&queue_obj->sender_list);
	tiny_init_list(&queue_obj->receiver_list);
	queue_obj->ring_queue = ring_queue;
	return queue_obj;
}

/// @brief 释放队列
/// @param queue 待释放的队列
void tiny_queue_plus_release(tiny_queue_plus_t *queue)
{
	if (queue)
	{
		tiny_wakeup_all_list_thread(&queue->sender_list);
		tiny_wakeup_all_list_thread(&queue->receiver_list);
		tiny_ring_queue_free(queue->ring_queue);
		tiny_free(queue);
	}
}

/// @brief 发送数据到队列
/// @param queue 待发送的队列
/// @param data 待发送的数据
/// @param size 发送数据大小, 如果大于块大小, 则直接返回
/// @param timeout 超时时间 小于于0表示无限等待
/// @return 执行状态
QUEUE_RET_STATUS tiny_queue_plus_send(tiny_queue_plus_t *queue, tiny_uint8_t *data, tiny_uint32_t size, tiny_int32_t timeout)
{
	if (!queue)
	{
		return QUEUE_ERROR;
	}

	while (1)
	{
		int flag = disable_interrupt();
		QUEUE_RET_STATUS ret = tiny_ring_queue_push(queue->ring_queue, data, size);

		if (ret == QUEUE_ERROR)
		{
			enable_interrupt(flag);
			return QUEUE_ERROR;
		}

		if (ret == QUEUE_OK)
		{
			tiny_wakeap_one_list_thread(&queue->receiver_list, tiny_true);
			enable_interrupt(flag);
			return QUEUE_OK;
		}

		if (timeout == 0)
		{
			enable_interrupt(flag);
			return QUEUE_FULL;
		}

		if (0 == tiny_suspend_thread_in_dst_list(get_self_thread(), &queue->sender_list))
		{
			enable_interrupt(flag);
			return QUEUE_ERROR;
		}

		tiny_thread_t *cur_running_thread = get_self_thread();
		cur_running_thread->timer_ret = THREAD_BLOCK_NOT_TIMEOUT;
		if (timeout > 0)
		{
			tiny_timer_set_interval(&cur_running_thread->timer, (tiny_uint32_t)CONVERT_MS_TO_TICK(timeout));
			tiny_timer_start(&cur_running_thread->timer);
			cur_running_thread->statu = THREAD_BLOCKED;
		}
		enable_interrupt(flag);

		schedule();

		if (cur_running_thread->timer_ret == THREAD_BLOCK_TIMEOUT)
		{
			return QUEUE_FULL;
		}
	}
}

/// @brief 接收队列数据
/// @param queue 待接收的队列
/// @param data 存储接收到的数据
/// @param size 存储数据大小, 如果大于块大小, 则直接返回
/// @param timeout 超时时间 小于于0表示无限等待
/// @return 执行状态
QUEUE_RET_STATUS tiny_queue_plus_recv(tiny_queue_plus_t *queue, tiny_uint8_t *data, tiny_uint32_t size, tiny_int32_t timeout)
{
	if (!queue)
	{
		return QUEUE_ERROR;
	}

	while (1)
	{
		int flag = disable_interrupt();
		QUEUE_RET_STATUS ret = tiny_ring_queue_pop(queue->ring_queue, data, size);

		if (ret == QUEUE_ERROR)
		{
			enable_interrupt(flag);
			return QUEUE_ERROR;
		}

		if (ret == QUEUE_OK)
		{
			tiny_wakeap_one_list_thread(&queue->sender_list, tiny_true);
			enable_interrupt(flag);
			return QUEUE_OK;
		}

		if (timeout == 0)
		{
			enable_interrupt(flag);
			return QUEUE_EMPTY;
		}

		if (0 == tiny_suspend_thread_in_dst_list(get_self_thread(), &queue->receiver_list))
		{
			enable_interrupt(flag);
			return QUEUE_EMPTY;
		}

		tiny_thread_t *cur_running_thread = get_self_thread();
		cur_running_thread->timer_ret = THREAD_BLOCK_NOT_TIMEOUT;
		if (timeout > 0)
		{
			tiny_timer_set_interval(&cur_running_thread->timer, (tiny_uint32_t)CONVERT_MS_TO_TICK(timeout));
			tiny_timer_start(&cur_running_thread->timer);
			cur_running_thread->statu = THREAD_BLOCKED;
		}
		enable_interrupt(flag);

		schedule();

		if (cur_running_thread->timer_ret == THREAD_BLOCK_TIMEOUT)
		{
			return QUEUE_EMPTY;
		}
	}
}

/// @brief 在中断中发送队列数据
/// @param queue 待发送的队列
/// @param data 待发送的数据
/// @param size 发送数据大小, 如果大于块大小, 则直接返回
/// @param[out] is_shcduled 是否发送调度
/// @return 执行状态
QUEUE_RET_STATUS tiny_queue_plus_send_irq(tiny_queue_plus_t *queue, tiny_uint8_t *data, tiny_uint32_t size, tiny_ret_status_t *is_shcduled)
{
	if (!queue)
	{
		return QUEUE_ERROR;
	}

	if (is_shcduled)
	{
		*is_shcduled = tiny_false;
	}

	int flag = disable_interrupt();
	QUEUE_RET_STATUS ret = tiny_ring_queue_push(queue->ring_queue, data, size);

	if (ret == QUEUE_ERROR)
	{
		enable_interrupt(flag);
		return QUEUE_ERROR;
	}

	if (ret == QUEUE_OK)
	{

		if (queue->receiver_list.len)
		{
			tiny_thread_t *wake_thread = (tiny_thread_t *)tiny_wakeap_one_list_thread(&queue->receiver_list, tiny_false);
			tiny_thread_t *self_thread = (tiny_thread_t *)get_self_thread();
			if (is_shcduled && wake_thread && wake_thread->cur_priority > self_thread->cur_priority)
			{
				*is_shcduled = tiny_true;
			}
		}

		enable_interrupt(flag);
		return QUEUE_OK;
	}

	enable_interrupt(flag);
	return QUEUE_ERROR;
}

/// @brief 在中断中接收队列数据
/// @param queue 待接收的队列
/// @param data 存储接收到的数据
/// @param size 存储数据大小, 如果大于块大小, 则直接返回
/// @param[out] is_shcduled 是否发送调度
/// @return 执行状态
QUEUE_RET_STATUS tiny_queue_plus_recv_irq(tiny_queue_plus_t *queue, tiny_uint8_t *data, tiny_uint32_t size, tiny_ret_status_t *is_shcduled)
{
	if (!queue)
	{
		return QUEUE_ERROR;
	}

	if (is_shcduled)
	{
		*is_shcduled = tiny_false;
	}

	int flag = disable_interrupt();
	QUEUE_RET_STATUS ret = tiny_ring_queue_pop(queue->ring_queue, data, size);

	if (ret == QUEUE_ERROR)
	{
		enable_interrupt(flag);
		return QUEUE_ERROR;
	}

	if (ret == QUEUE_OK)
	{

		if (queue->sender_list.len)
		{
			tiny_thread_t *wake_thread = (tiny_thread_t *)tiny_wakeap_one_list_thread(&queue->sender_list, tiny_false);
			tiny_thread_t *self_thread = (tiny_thread_t *)get_self_thread();
			if (is_shcduled && wake_thread && wake_thread->cur_priority > self_thread->cur_priority)
			{
				*is_shcduled = tiny_true;
			}
		}

		enable_interrupt(flag);
		return QUEUE_OK;
	}

	enable_interrupt(flag);
	return QUEUE_ERROR;
}
